<?php
// 本类由系统自动生成，仅供测试用途
namespace Backend\Controller;
use MyBase\BaseController;
use MyBase\Tool;
class JoinController extends BaseController {

    //检查是否登录
    public function _before_index(){
//     	session("admin",null);
        if(!session("admin")){
            $this->redirect('adminLogin');
        }
        //看是否曾经记住过
        if(cookie("admin_cookie")){
        	$this->assign("admin_cookie",cookie('admin_cookie'));
        }
    }

    /**
     * 异步加载添加所用的表单
     */
    public function ajaxAddJoinForm(){
        layout(false);
        $this->display("Index/join_add");
    }

    /**
     * 异步加载join_type
     */
    public function ajaxLoadJoinType(){
        $res = $this->JoinTypeModel->getTypeList();
        $this->ajaxReturn(true,'获取列表成功',$res);
    }

    /**
     * 异步添加
     */
    public function ajaxAddJoin(){
        $postData = I('post.');
        $postData['join_date'] = time();
        $res = $this->JoinModel->addJoin($postData);
        //移动logo图片
        $tempImgPath  = './Uploads/temp/'.$postData['join_logo'];
        if(!file_exists($tempImgPath)){
            $this->ajaxReturn(false,'图片丢失');
        }
        $newImgPath = './Uploads/join/'.$postData['join_date'].$res.'/'.$postData['join_logo'];
        $indexController = A("Index");
        $indexController->copy_move($tempImgPath,$newImgPath);
        if(!$res){
            $this->ajaxReturn(false,'添加失败');
        }
        $this->ajaxReturn(true,'添加成功');
    }

    /**
     * 异步获取加盟列表
     * 加盟列表记录不是很多，这里不采用分页，采用更多的方式
     */
    public function ajaxGetJoinList(){
        layout(false);
        $postData = I('post.');
        $pageLimit = 4;
        $pageData = $this->JoinModel->getJoinList($postData['offset'],$pageLimit);
        foreach ($pageData as $key => $value) {
            $pageData[$key]['logo_src'] = __ROOT__.'/Uploads/join/'. $pageData[$key]['join_date']. $pageData[$key]['join_id'].'/'.$pageData[$key]['join_logo'];
        }
        $this->assign('page_data',$pageData);   
        $pageDataCount = $this->JoinModel->getJoinListNum();
        $this->assign('page_data_count',$pageDataCount);
        //计算下一页的offset
        if(($postData['offset'] + $pageLimit) >= $pageDataCount){
            $offset = $pageDataCount;
        }else{
            $offset = $postData['offset'] + $pageLimit;
        }
        $this->assign('offset',$offset);
        $this->display("Index/join_list_page");
    }

    public function ajaxGetJoinEditPage(){
        layout(false);
        $postData = I('post.');
        $res = $this->JoinModel->getJoinInfoById($postData['join_id']);
        $res['logo_src'] = __ROOT__.'/Uploads/join/'. $res['join_date']. $res['join_id'].'/'.$res['join_logo'];
        $this->assign('join_edit_data',$res);
        $typeList = $this->JoinTypeModel->getTypeList();
        $this->assign('join_type_list',$typeList);
        $this->display("Index/join_edit_page");
    }
    public function ajaxUpdateJoin(){
        $postData = I('post.');
        foreach ($postData as $key => $value) {
            $key_ = str_replace('_edit','',$key);
            unset($postData[$key]);
            $postData[$key_] = $value;
        }
        // print_r($postData);
        if($postData['join_logo'] ==''){
            unset($postData['join_logo']);
        }else{
           $data['join_logo'] = $postData['join_logo']; 
            //如果图片呗修改，则移动图片
            $tempImgPath  = './Uploads/temp/'.$postData['join_logo'];
            // if(!file_exists($tempImgPath)){
            //     $this->ajaxReturn(false,'图片丢失');
            // }
            $editData = $this->JoinModel->getJoinInfoById($postData['join_id']);
            $newImgPath = './Uploads/join/'.$editData['join_date'].$editData['join_id'].'/'.$postData['join_logo'];
            // echo $newImgPath;
            $indexController = A("Index");
            $indexController->copy_move($tempImgPath,$newImgPath);
        }
        $res = $this->JoinModel->save($postData);
        if($res === false){
            $this->ajaxReturn(false,'更新失败');
        }else{
             $this->ajaxReturn(true,'更新成功');
        }
    }
}